package com.chezhujie.www.utils;

import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * Created by 汤伟杰 on 2017/5/24.
 */
public class Fileupload {
    public static boolean createDir(String destDirName) {
        if(StringUtils.isNotBlank(destDirName)) {
            File dir = new File(destDirName);
            if (dir.exists()) {
//                System.out.println("创建目录" + destDirName + "失败，目标目录已经存在");
                return false;
            }
            if (!destDirName.endsWith(File.separator)) {
                destDirName = destDirName + File.separator;
            }
            //创建目录
            if (dir.mkdirs()) {
                System.out.println("创建目录" + destDirName + "成功！");
                return true;
            } else {
                System.out.println("创建目录" + destDirName + "失败！");
                return false;
            }
        }
        return false;
    }

    public static void deleteFile(HttpServletRequest req, String picPath) {
        if(StringUtils.isNotBlank(picPath)){
            String basePath = req.getSession().getServletContext().getRealPath("/");
            File file = new File(basePath+picPath);
            if(file.exists()&&file.isFile()){
                file.delete();
            }
        }

    }
}
